
/**
 *
 */
package com.serviceprovider.business.module.admin.transaction.utility;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.transaction.model.TransactionTarget;

//~--- JDK imports ------------------------------------------------------------

import java.io.IOException;

import java.util.Date;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

/**
 * @author mrduongnv@gmail.com
 *
 */
public class TragetDataTag implements Tag {

    /** The parent. */
    private Tag parent = null;

    /** The pc. */
    private PageContext pc = null;

    /** The target data list. */
    List<TransactionTarget> targetDataList = null;

    /*
     * (non-Javadoc)
     *
     * @see
     * javax.servlet.jsp.tagext.Tag#setPageContext(javax.servlet.jsp.PageContext
     * )
     */
    public void setPageContext(PageContext p) {
        this.pc = p;
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.servlet.jsp.tagext.Tag#setParent(javax.servlet.jsp.tagext.Tag)
     */
    public void setParent(Tag t) {
        this.parent = t;
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.servlet.jsp.tagext.Tag#getParent()
     */
    public Tag getParent() {
        return this.parent;
    }

    /**
     * Gets the target data list.
     *
     * @return the targetDataList
     */
    public List<TransactionTarget> getTargetDataList() {
        return this.targetDataList;
    }

    /**
     * Sets the target data list.
     *
     * @param targetDataList
     *            the targetDataList to set
     */
    public void setTargetDataList(List<TransactionTarget> targetDataList) {
        this.targetDataList = targetDataList;
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
     */
    public int doStartTag() throws JspException {
        try {
            if (this.targetDataList != null) {
                this.pc.getOut().write(createTable(this.targetDataList));
            }
        } catch (IOException e) {
            throw new JspTagException("An IOException occurred.");
        }

        return SKIP_BODY;
    }

    private String createTable(List<TransactionTarget> targetDataList) {
        StringBuilder builder = new StringBuilder();

        builder.append("<table width=\"100%\" class=\"formTable\">");
        builder.append("<tbody>");
        builder.append("<tr class=\"formHeader\">");
        builder.append("<td>Transaction Id</td>");
        builder.append("<td>User Id </td>");
        builder.append("<td>Customer Id</td>");
        builder.append("<td>Transaction Amount </td>");
        builder.append("<td>Transaction Status Id</td>");

//      builder.append("<td>Service Fee</td>");
//      builder.append("<td>Self Fee</td>");
//      builder.append("<td>Tax prep Fee</td>");
//      builder.append("<td>Electronic Prep Fee</td>");
//      builder.append("<td>Electronic Filling Fee</td>");
//      builder.append("<td>Service Bureau Fee</td>");
//      builder.append("<td>Fed Bank Product Fee</td>");
//      builder.append("<td>State Product Fee</td>");
//      builder.append("<td>Custom Label 1 Value</td>");
//      builder.append("<td>Custom Label 2 Value</td>");
        builder.append("<td>Customer balance</td>");

//      builder.append("<td>Payee 1 Amount</td>");
//      builder.append("<td>Payee 2 Amount</td>");
//      builder.append("<td>Payee 3 Amount</td>");
//      builder.append("<td>Payee 4 Amount</td>");
//      builder.append("<td>Batch Id</td>");
        builder.append("<td>Data Load Date</td>");
        builder.append("<td>Receiver Name</td>");
        builder.append("<td>Originator Name</td>");
        builder.append("<td>Customer SSN</td>");
        builder.append("<td>Trace Number</td>");
        builder.append("<td>Processed</td>");
        builder.append("</tr>");

        for (TransactionTarget transactionTarget : targetDataList) {
            createTR(builder, transactionTarget);
        }

        builder.append("</tbody>");
        builder.append("</table>");

        return builder.toString();
    }

    private void createTR(StringBuilder builder, TransactionTarget transactionTarget) {
        builder.append("<tr>");
        createTD(builder, transactionTarget);
        builder.append("</tr>");
    }

    private void createTD(StringBuilder builder, TransactionTarget transactionTarget) {
        createRecord(builder, (transactionTarget.getTransaction_id() == null)
                              ? ""
                              : transactionTarget.getTransaction_id().toString(), Integer.class);
        createRecord(builder, (transactionTarget.getUser_id() == null)
                              ? ""
                              : transactionTarget.getUser_id().toString(), Integer.class);
        createRecord(builder, (transactionTarget.getCustomer_id() == null)
                              ? ""
                              : transactionTarget.getTrace_number().toString(), Integer.class);
        createRecord(builder, (transactionTarget.getTransaction_amount() == null)
                              ? ""
                              : transactionTarget.getTransaction_amount().toString(), Integer.class);
        createRecord(builder, (transactionTarget.getTransaction_status_id() == null)
                              ? ""
                              : transactionTarget.getTransaction_status_id().toString(), Integer.class);

//      createRecord(builder, transactionTarget.getService_fee() == null ? "" : transactionTarget.getService_fee().toString(), Integer.class);
//      createRecord(builder, transactionTarget.getSelf_fee() == null ? "" : transactionTarget.getSelf_fee().toString(), Integer.class);
//      createRecord(builder, transactionTarget.getTax_prep_fee() == null ? "" : transactionTarget.getTax_prep_fee().toString(), Integer.class);
//      createRecord(builder, transactionTarget.getElectronic_prep_fee() == null ? "" : transactionTarget.getElectronic_prep_fee().toString(),
//                      Integer.class);
//      createRecord(builder, transactionTarget.getElectronic_filling_fee() == null ? "" : transactionTarget.getElectronic_filling_fee().toString(),
//                      Integer.class);
//      createRecord(builder, transactionTarget.getService_bureau_fee() == null ? "" : transactionTarget.getService_bureau_fee().toString(),
//                      Integer.class);
//      createRecord(builder, transactionTarget.getFed_bank_product_fee() == null ? "" : transactionTarget.getFed_bank_product_fee().toString(),
//                      Integer.class);
//      createRecord(builder, transactionTarget.getState_product_fee() == null ? "" : transactionTarget.getState_product_fee().toString(),
//                      Integer.class);
//      createRecord(builder, transactionTarget.getCustom_label_1_value() == null ? "" : transactionTarget.getCustom_label_1_value().toString(),
//                      Integer.class);
//      createRecord(builder, transactionTarget.getCustom_label_2_value() == null ? "" : transactionTarget.getCustom_label_2_value().toString(),
//                      Integer.class);
        createRecord(builder, (transactionTarget.getCustomer_balance() == null)
                              ? ""
                              : transactionTarget.getCustomer_balance().toString(), Integer.class);

//      createRecord(builder, transactionTarget.getPayee_1_amount() == null ? "" : transactionTarget.getPayee_1_amount().toString(), Integer.class);
//      createRecord(builder, transactionTarget.getPayee_2_amount() == null ? "" : transactionTarget.getPayee_2_amount().toString(), Integer.class);
//      createRecord(builder, transactionTarget.getPayee_3_amount() == null ? "" : transactionTarget.getPayee_3_amount().toString(), Integer.class);
//      createRecord(builder, transactionTarget.getPayee_4_amount() == null ? "" : transactionTarget.getPayee_4_amount().toString(), Integer.class);
//      createRecord(builder, transactionTarget.getBatch_id() == null ? "" : transactionTarget.getBatch_id().toString(), Integer.class);
        createRecord(builder, (transactionTarget.getData_load_date() == null)
                              ? ""
                              : StringUtils.formatDate(transactionTarget.getData_load_date()), Date.class);
        createRecord(builder, (transactionTarget.getReceiver_name() == null)
                              ? ""
                              : transactionTarget.getReceiver_name().toString(), String.class);
        createRecord(builder, (transactionTarget.getOriginator_name() == null)
                              ? ""
                              : transactionTarget.getOriginator_name().toString(), String.class);
        createRecord(builder, (transactionTarget.getCustomer_ssn() == null)
                              ? ""
                              : transactionTarget.getCustomer_ssn().toString(), Integer.class);
        createRecord(builder, (transactionTarget.getTrace_number() == null)
                              ? ""
                              : transactionTarget.getTrace_number().toString(), String.class);
        createRecord(builder, (transactionTarget.isProcessed() == true)
                              ? "yes"
                              : "no", String.class);
    }

    private void createRecord(StringBuilder builder, String string, Class c) {
        if (c.equals(Integer.class)) {
            if ((string != null) &&!string.trim().isEmpty() &&!StringUtils.isAllDigits(string)) {
                builder.append("<td style=\"background-color: red\">");
            } else {
                builder.append("<td>");
            }
        } else if (c.equals(Date.class)) {
            if ("-1".equals(string)) {
                builder.append("<td style=\"background-color: red\">");
            } else {
                builder.append("<td>");
            }
        } else {
            builder.append("<td>");
        }

        builder.append(string);
        builder.append("</td>");
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.servlet.jsp.tagext.Tag#doEndTag()
     */
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.servlet.jsp.tagext.Tag#release()
     */
    public void release() {
        this.pc             = null;
        this.parent         = null;
        this.targetDataList = null;
    }
}



